/* BONE.C */

/*
	Copyright (C) 2010 by Lesnikov Andrey <ozkriff@gmail.com>
	This file is part of ozkriff_game_2.

	ozkriff_game_2 is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	ozkriff_game_2 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with ozkriff_game_2.  If not, see <http://www.gnu.org/licenses/>.

	See the COPYING file for more details.
*/

#if(0)
#define MAX_CHCOUNT 20

typedef struct Bone {
	float a, l;
	float rel[16];
	
	int childCount;
	struct	Bone *child[MAX_CHCOUNT],
				*parent;
} Bone;

typedef struct vertex {
	float pos[3];
	Bone *bone;
} vertex;



Bone*
boneAddChild(Bone *root, float a, float l)
{
	Bone *tmp_bone;
	int i;
	
	if (!root) { /* If there is no root, create one */
		if (!(root = (Bone *)malloc(sizeof(Bone))))
			return(NULL);
		root->parent = NULL;
	} else if (root->childCount < MAX_CHCOUNT) { /* If there is space for another child */
		/* Allocate the child */
		if (!(tmp_bone = (Bone *)malloc(sizeof(Bone))))
			return(NULL);
			
		tmp_bone->parent = root; /* Set it's parent */
		/* Increment the childCounter and set the pointer */
		root->child[root->childCount++] = tmp_bone;
		root = tmp_bone; /* Change the root */
	} else /* Can't add a child */
		return(NULL);
		
	/* Set data */
	root->a = a;
	root->l = l;
	root->childCount = 0;
	
	for (i = 0; i < MAX_CHCOUNT; i++)
		root->child[i] = NULL;
		
	return(root); //return bone, that we just have added!
}




void
boneDraw(Bone *root)
{
	glPushMatrix();
	
	glRotatef(root->a, 0, 0, 1);
	
	glGetFloatv(GL_MODELVIEW_MATRIX, root->rel);
	
	glBegin(GL_TRIANGLES);
	//glBegin(GL_LINES);
	{
		glColor3f(1.0, 0.0, 0.0);
		glVertex2f(0, -5);
		glVertex2f(0, +5);
		//glVertex2f(0, 0);
		glColor3f(1.0, 1.0, 1.0);
		glVertex2f(root->l, 0);
	}
	glEnd();
	
	/* Translate to reach the new starting position */
	glTranslatef(root->l, 0.0, 0.0);
	
	int i;
	/* Call function on bones children */
	for (i = 0; i < root->childCount; i++)
		boneDraw(root->child[i]);
		
	glPopMatrix();
}



void
rotate_vertex(float *m, float *pos)
{
	float v[3] = {pos[0], pos[1], pos[2]};
	
	pos[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8];
	pos[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9];
	pos[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10];
}
#endif


